﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
//using UnityEditor.UI;
using UnityEditor;
using UnityEngine;

namespace UFrame.AutoQuote
{
    public partial class AutoQuoteMenu
    {
        private static bool IsClear = false;

        [MenuItem("Plateface/AutoRef/OpenRefWindow")]
        public static void OpenWindow()
        {
            EditorWindow.GetWindow<AutoQuoteEditorWindow>(false, "AutoRef", false);
        }

        [MenuItem("Plateface/AutoRef/SetName# &3")]
        public static void SetGameName()
        {
            var goList = Selection.gameObjects;
            //GameObject o = Selection.activeGameObject;
            //if (o == null)
            //    return;

            foreach (var o in goList)
            {
                if (o == null)
                    continue;

                string name = o.name;
                if (name.StartsWith("#"))
                {
                    name = name.Substring(1);
                    o.name = name;
                }
                else
                {
                    name = name.Insert(0, "#");
                    o.name = name;
                }
            }
        }


        [MenuItem("Plateface/AutoRef/SetRef &2")]
        public static void SetRefMenu()
        {
            GameObject o = Selection.activeGameObject;
            if (o == null)
                return;

            var list = GetAutoQuoteList(o);
            foreach (var c in list)
            {
                AutoSetRef(c, c.gameObject, IsClear);
            }

            IsClear = !IsClear;
            /*PrefabUtility.SetPropertyModifications(o,new PropertyModification[]
            {
                
            });*/
            
         
                

            if (PrefabUtility.IsAnyPrefabInstanceRoot(o))
            {
                try
                {
                    PrefabUtility.SavePrefabAsset(o);
                }
                catch (Exception ex)
                {
                    
                }
                
                EditorUtility.SetDirty(o);
                PrefabUtility.ApplyPrefabInstance(o,InteractionMode.AutomatedAction);
                
            }
            else
            {
                var root = PrefabUtility.GetNearestPrefabInstanceRoot(o);
                if (root != null)
                {
                    EditorUtility.SetDirty(o);
                }
            }



            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            if (AutoQuoteCall.setRefDel != null) AutoQuoteCall.setRefDel(IsClear);
        }


        [MenuItem("Plateface/AutoRef/Generate Code NGUI &4")]
        public static void AutoCreateCodeMenu()
        {
            GameObject o = Selection.activeGameObject;
            if (o == null)
                return;

            //AutoGenerateSet.IsNGUI = true;
            var list = GetAutoQuoteList(o);

            foreach (var item in list)
            {
                AutoCreateCode(item);
            }

            if (AutoQuoteCall.createCodeDel != null) AutoQuoteCall.createCodeDel();
        }


        private static List<Component> GetAutoQuoteList(GameObject o)
        {
            List<Component> cList = new List<Component>();
            List<Component> newList = new List<Component>();

            o.GetComponentsInChildren<Component>(true, cList);
            for (int i = cList.Count - 1; i >= 0; i--)
            {
                if (cList[i] == null)
                    continue;

                bool isGenerate = IsAutoQuote(cList[i].GetType());

                if (isGenerate)
                {
                    newList.Add(cList[i]);
                }
            }

            return newList;
        }


        public static bool IsAutoQuote(System.Type type)
        {
            foreach (var scriptType in generateScript)
            {
                if (type.FullName == scriptType.FullName)
                {
                    return true;
                }

                if (AutoQuoteTool.IsSubClass(scriptType, type))
                {
                    return true;
                }
            }


            if (AutoQuoteTool.IsHasAttribute<AAutoQuote>(type))
            {
                return true;
            }

            return false;
        }
    }
}


#endif